<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box">
    <div>
      <!-- 搜索 -->
      <el-form
        ref="formSearch"
        class="search"
        :inline="true"
        :model="formSearch"
      >
        <el-form-item
          prop="truckTypeId"
          label="车辆类型："
          label-width="120px"
        >
          <el-select
            v-model="formSearch.truckTypeId"
            style="width: 400px;"
          >
            <el-option
              v-for="item in selectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="licensePlate"
          label="车牌号码："
          label-width="120px"
        >
          <el-input
            v-model="formSearch.licensePlate"
            style="width: 400px;"
            placeholder="请输入车牌号码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="warning"
            @click="search"
          >搜索</el-button>
          <el-button
            class="reset-btn"
            @click="resetSearch"
          >重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 导航 -->
      <TabChange
        class="navRow"
        :default-active-index="showStatus"
        :setting-list="[
          {
            value: 0,
            label: '全部',
            num: countAll
          },
          {
            value: 1,
            label: '可用',
            num: countOne
          },
          {
            value: 2,
            label: '停用',
            num: countTwo
          }
        ]"
        @tabChange="tabChange"
      ></TabChange>
      <!-- 表格区域 -->
      <div class="customer-details-box tabel">
        <el-button
          type="warning"
          class="addBtn"
          @click="dialogAdd = true"
        >新增车辆</el-button>
        <!-- 表格内容  -->
        <el-table
          size="small"
          :stripe="true"
          :data="tableData"
          style="width: 100%"
          :row-class-name="tableRowClassName"
        >
          <el-table-column
            type="index"
            :index="hindex"
            label="序号"
            width="60"
          >
          </el-table-column>
          <el-table-column
            prop="licensePlate"
            label="车牌号"
            width="160"
          >
          </el-table-column>
          <el-table-column
            prop="truckTypeName"
            label="车辆类行"
          >
          </el-table-column>
          <el-table-column
            prop="driverNum"
            label="司机数量"
          >
          </el-table-column>
          <el-table-column
            prop="workStatus"
            label="车辆状态"
          >
            <template v-slot="{ row }">
              <span> {{ row.workStatus === 1? '可用' : '停用' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="deviceGpsId"
            label="GPS设备ID"
          >
          </el-table-column>
          <el-table-column
            prop="allowableLoad"
            label="实载重量(T)"
          >
          </el-table-column>
          <el-table-column
            prop="allowableVolume"
            label="实载重量(方)"
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="操作"
            width="250px"
          >
            <template v-slot="{ row }">
              <el-button
                type="text"
                @click="jump(row.id)"
              >查看详情</el-button><span class="line">    |</span>
              <el-button
                type="text"
                @click="enableDialog"
              >启用</el-button><span class="line">    |</span>
              <el-button
                type="text"
                @click="driverConfig(row.id)"
              >配置司机</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tatolPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 弹窗 -->
    <el-dialog
      width="32%"
      title="新增车辆"
      :visible.sync="dialogAdd"
      @close="close"
    >
      <el-form
        ref="addform"
        :model="addForm"
        :rules="addrules"
      >
        <el-form-item
          prop="truckTypeId"
          label="车辆类型"
          label-width="120px"
        >
          <el-select
            v-model="addForm.truckTypeId"
          >
            <el-option
              v-for="item in selectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="licensePlate"
          label="车牌号码"
          label-width="120px"
        >
          <el-input
            v-model="addForm.licensePlate"
            placeholder="请输入车牌号码"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="deviceGpsId"
          label="GPS设备ID"
          label-width="120px"
        >
          <el-input
            v-model="addForm.deviceGpsId"
            placeholder="请输入GPS设备ID"
          ></el-input>
        </el-form-item>
        <el-row style="height: 80px;">
          <el-button
            type="warning"
            @click="addTransit"
          >确认</el-button>
          <el-button
            class="reset-btn"
            @click="close"
          >取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 启用弹窗 -->
    <el-dialog
      class="dialog"
      :title="enableList.title"
      width="30%"
      :visible.sync="showdialog"
    >
      <div class="title">{{ enableList.inquire }} </div>
      <el-row
        type="flex"
        style="height: 30px;"
      >
        <el-col>1、车辆信息已完善</el-col>
        <el-col>2、{{ enableList.second }}</el-col>
      </el-row>
      <el-row
        type="flex"
        justify="end"
        style="height: 50px;"
      >
        <el-col :span="6">
          <el-button
            size="mini"
            @click="closeDialog"
          >取消</el-button>
          <el-button
            size="mini"
            style="background-color: #e15536; color: #fff;"
            @click="btnOk"
          >确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 配置司机 -->
    <Arrange
      ref="arrange"
      :title-info="titleInfo"
      :car-modal-info="carModalInfo"
      :form-base-ad="formBaseAd"
      :drivers-list="driversList"
    ></Arrange>
  </div>
</template>
<script>
import Arrange from './components/arrange-driver.vue'
import TabChange from '@/components/TabChange'
// 引入api
// import { getTruckList } from '@/api/vehicle'
import { truckList, getCarTypeList, truckTypeAdd, truckTypeDetail, unBindDriverList, truckStatusUse } from '@/api/transit'
export default {
  name: 'Vehicle',
  components: {
    TabChange, Arrange
  },
  data() {
    return {
      driversList: [],
      formBaseAd: {},
      carModalInfo: {},
      carId: null,
      titleInfo: { text: '配置司机' },
      showdialog: false,
      enableList: {
        code: null,
        title: '车辆启用',
        inquire: '确定要启用嘛？车辆需满足以下条件：',
        second: '绑定司机>=2,且有排班'
      },
      addForm: {
        truckTypeId: '', // 新增车辆类型
        licensePlate: '', // 新增车牌号码
        deviceGpsId: null // 新增GPSIND
      },
      addrules: {
        truckTypeId: [{ required: true, message: '新增车辆类型不能为空', trigger: 'blur' }],
        licensePlate: [{ required: true, message: '新增车辆类型不能为空', trigger: 'blur' },
          { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}[A-HJ-NP-Z]{1}[0-9]{5}$/,
            message: '请输入正确的车牌号码',
            trigger: 'blur'
          }],
        deviceGpsId: [{ required: true, message: '新增车辆类型不能为空', trigger: 'blur' }]
      },
      dialogAdd: false,
      selectList: [],
      activeIndex: null,
      tatolPage: null,
      currentPage: null,
      countAll: null, // 全部
      countOne: null, // 可用
      countTwo: null, // 停用
      showStatus: '', // 控制高亮与否
      query: {
        page: 1,
        pageSize: 10
      },
      formSearch: {
        licensePlate: '', // 车牌号码
        truckTypeId: '' // 车辆类型
      },
      tableData: []// 渲染的列表
    }
  },
  computed: {
    // 序号
    hindex() {
      return (this.query.page - 1) * (this.query.pageSize) + 1
    }
  },
  created() {
    this.getTruckList()
    this.getSelect()
  },
  methods: {
    // 查看详情跳转
    jump(id) {
      this.$router.push(`/transit/vehicle-detail/${id}`)
    },
    // 确定按钮
    async btnOk() {
      if (this.enableList.code === 2) {
        this.$refs.arrange.dialogFormVisible = true
        const { data } = await truckTypeDetail(this.carId)
        this.carModalInfo = data
        const res = await unBindDriverList()
        this.driversList = res.data
        this.showdialog = false
      } else {
        await truckStatusUse(this.carId)
        this.$message.success('启动成功')
        this.$refs.arrange.dialogFormVisible = false
      }
    },
    // 司机配置
    driverConfig(id) {
      this.carId = id
      this.showdialog = true
      this.enableList.code = 2
      this.enableList.title = '车辆配置'
      this.enableList.inquire = '配置司机需满足以下条件：'
      this.enableList.second = '车辆未完成运输任务'
    },
    // 启用弹窗
    enableDialog() {
      this.enableList.code = 1
      this.enableList.title = '车辆启用'
      this.enableList.inquire = '确定要启用嘛？车辆需满足以下条件：'
      this.enableList.second = '绑定司机>=2,且有排班'
      this.showdialog = true
    },
    // 关闭启用/配置弹窗函数
    closeDialog() {
      this.showdialog = false
    },
    // 重置搜索框
    resetSearch() {
      this.$refs.formSearch.resetFields()
    },
    // 关闭弹窗
    close() {
      this.dialogAdd = false
      this.$refs.addform.resetFields()
    },
    // 确定新增按钮
    async addTransit() {
      await truckTypeAdd(this.addForm)
      this.$message.success('新增成功')
      this.close()
    },
    // 搜索按钮
    search() {
      this.query.page = 1
      this.activeIndex = 3
      this.getTruckList(this.formSearch)
    },
    // 下拉框的数据
    async getSelect() {
      const res = await getCarTypeList()
      this.selectList = res.data
    },
    // 分页渲染
    handleSizeChange(val) {
      this.query.pageSize = val
      this.getTruckList()
    },
    handleCurrentChange(val) {
      this.query.page = val
      if (this.activeIndex === 1) {
        this.getTruckList({ workStatus: 1 })
      } else if (this.activeIndex === 2) {
        this.getTruckList({ workStatus: 0 })
      } else if (this.activeIndex === 3) {
        this.getTruckList(this.formSearch)
      } else {
        this.getTruckList()
      }
    },
    // 获取数据渲染列表
    async getTruckList(obj) {
      const { data } = await truckList({ ...this.query, ...obj })
      this.tableData = data.items
      this.tatolPage = +data.counts
    },
    // 导航切换渲染
    tabChange(activeIndex) {
      this.query.page = 1
      this.activeIndex = activeIndex
      if (activeIndex === 0) {
        this.getTruckList()
      } else if (activeIndex === 1) {
        this.getTruckList({ workStatus: 1 })
      } else {
        this.getTruckList({ workStatus: 0 })
      }
    },
    onSubmit() {
      console.log('submit!')
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    }
  }

}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.dialog{
  padding-left: 20px;
  .title{
    padding-left: 15px;
    margin-bottom: 10px;
    font-size: 16px;
    text-align: left;
  }
}
.reset-btn{
  width: 100px;
  height: 40px;
  padding: 12px 20px;
  font-size: 14px;
  border-radius: 4px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.vehicle ::v-deep .el-table td,
.el-table th {
  font-size: 13px;
  padding: 8px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  .tabel{
    margin: 30px 15px 0 15px;
    padding: 20px;
    .el-pagination{
      height: 100px;
      font-weight: 500;
      text-align: center;
      margin-top: 20px;
      font-size: 14px;
    }
    .line{
      color:#dcdfe6 ;
    }
    .addBtn{
      margin-bottom: 15px;
    }
  }
  .navRow{
    margin: 20px 15px 0 15px;
  }
  .search{
    background-color: #fff;
    margin:10px 15px 0 15px;
    border-radius: 3px;
    height: 80px;
    padding: 20px 0;
    .demo-form-inline{
      padding: 0px;
    }
  }
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 20px 20px 0px 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}
</style>

<style>
  .startconfirm{
    width: 481px;

    }
    .startconfirm .el-message-box__header{
      padding-bottom: 4px;
    }
    .startconfirm .el-message-box__container{
      margin-bottom: 15px;
    }
    .confirm .el-message-box__status.el-icon-warning{
      left: 70px;
    }
    .confirm.el-message-box__title{
      font-size: 16px;
    }
    .confirm .el-message-box__message p {
      color: #F92D2D;
      margin-top: 5px;
    }
    .confirm .el-message-box__btns{
      text-align: center;
      margin-top: 10px;
    }
</style>
